package MonitorUI;

import Database.ZooDataHandler;
import Enclosure.*;
import javax.swing.*;
import java.awt.*;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.sql.SQLException;
import java.util.ArrayList;


public class EnclosureView extends JPanel {
    ZooDataHandler zooDataHandler = new ZooDataHandler();
    Enclosure enclosure;
    ArrayList<Entry> enclosureEntries;

    int [] backgroundPosition = {0, 0, 200, 200};
    int backgroundWidth = backgroundPosition[2];
    int backgroundHeight = backgroundPosition[3];
    int backgroundTop = backgroundPosition[1];
    int backgroundLeftSide = backgroundPosition[0];


    int [] enclosurePosition = {50, 50, 100, 100};
    int enclosureWidth = enclosurePosition[2];
    int enclosureHeight = enclosurePosition[3];
    int enclosureTop = enclosurePosition[1];
    int enclosureLeftSide = enclosurePosition[0];

    int [] doorOnePosition = {75, 45, 50, 10};
    int doorOneWidth = doorOnePosition[2];
    int doorOneHeight = doorOnePosition[3];
    int doorOneTop = doorOnePosition[1];
    int doorOneBottom = doorOnePosition[1] + doorOnePosition[3];
    int doorOneLeftSide = doorOnePosition[0];
    int doorOneRightSide = doorOnePosition[0] + doorOnePosition[2];

    int [] doorTwoPosition = {45, 75, 10, 50};
    int doorTwoWidth = doorTwoPosition[2];
    int doorTwoHeight = doorTwoPosition[3];
    int doorTwoTop = doorTwoPosition[1];
    int doorTwoBottom = doorTwoPosition[1] + doorTwoPosition[3];
    int doorTwoLeftSide = doorTwoPosition[0];
    int doorTwoRightSide = doorTwoPosition[0] + doorTwoPosition[2];

    int [] doorThreePosition = {145, 75, 10, 50};
    int doorThreeWidth = doorThreePosition[2];
    int doorThreeHeight = doorThreePosition[3];
    int doorThreeTop = doorThreePosition[1];
    int doorThreeBottom = doorThreePosition[1] + doorThreePosition[3];
    int doorThreeLeftSide = doorThreePosition[0];
    int doorThreeRightSide = doorThreePosition[0] + doorThreePosition[2];

    int [] animalIndicatorPosition = {65, 60, 15, 15};
    int animalIndicatorWidth = animalIndicatorPosition[2];
    int animalIndicatorHeight = animalIndicatorPosition[3];
    int animalIndicatorTop = animalIndicatorPosition[1];
    int animalIndicatorBottom = animalIndicatorPosition[1] + animalIndicatorPosition[3];
    int animalIndicatorLeftSide = animalIndicatorPosition[0];
    int animalIndicatorRightSide = animalIndicatorPosition[0] + animalIndicatorPosition[2];

    int [] humanIndicatorPosition = {120, 60, 15, 15};
    int humanIndicatorWidth = humanIndicatorPosition[2];
    int humanIndicatorHeight = humanIndicatorPosition[3];
    int humanIndicatorTop = humanIndicatorPosition[1];
    int humanIndicatorBottom = humanIndicatorPosition[1] + humanIndicatorPosition[3];
    int humanIndicatorLeftSide = humanIndicatorPosition[0];
    int humanIndicatorRightSide = humanIndicatorPosition[0] + humanIndicatorPosition[2];

    int [] lockPosition = {122, 127, 13, 15};
    int lockTop = lockPosition[1];
    int lockBottom = lockPosition[1] + lockPosition[3];
    int lockLeftSide = lockPosition[0];
    int lockRightSide = lockPosition[0] + lockPosition[2];

    int [] lightIndicatorPosition = {60, 132, 30, 11};
    int lightIndicatorTop = lightIndicatorPosition[1];
    int lightIndicatorBottom = lightIndicatorPosition[1] + lightIndicatorPosition[3];
    int lightIndicatorLeftSide = lightIndicatorPosition[0];
    int lightIndicatorRightSide = lightIndicatorPosition[0] + lightIndicatorPosition[2];

    public EnclosureView(Enclosure enclosure) throws Exception {
        this.enclosure = enclosure;
        enclosureEntries = enclosure.getEnclosureEntries();
        this.setSize(400, 400);

        this.addMouseListener(new MouseListener() {
            @Override
            public void mouseClicked(MouseEvent e) {}
            @Override
            public void mousePressed(MouseEvent e) {}
            @Override
            public void mouseReleased(MouseEvent e) {
                try {
                    itemClicked(e.getX(), e.getY());
                } catch (SQLException err) {
                    System.out.println(err);
                }
            }
            @Override
            public void mouseEntered(MouseEvent e) {}
            @Override
            public void mouseExited(MouseEvent e) {}
        });
    }


    public void itemClicked(int x, int y) throws SQLException {
        if (x > animalIndicatorLeftSide && x < animalIndicatorRightSide && y > animalIndicatorTop && y < animalIndicatorBottom) {
            if (enclosure.isEnclosureOccupiedByAnimal()) { enclosure.setEnclosureUnoccupiedByAnimal(); } else { enclosure.setEnclosureOccupiedByAnimal(); }
        }
        if (x > humanIndicatorLeftSide && x < humanIndicatorRightSide && y > humanIndicatorTop && y < humanIndicatorBottom) {
            if (enclosure.isEnclosureOccupiedByPerson()) { enclosure.setEnclosureUnoccupiedByPerson(); } else { enclosure.setEnclosureOccupiedByPerson(); }
        }
        if (x > lockLeftSide && x < lockRightSide && y > lockTop && y < lockBottom) {
            if (enclosure.isEnclosureLocked()) {
                enclosure.unlockEnclosure();
            } else {
                enclosure.closeAllEnclosureEntries();
                enclosure.lockEnclosure();
            }
        }
        if (x > lightIndicatorLeftSide && x < lightIndicatorRightSide && y > lightIndicatorTop && y < lightIndicatorBottom) {
            if (enclosure.isEnclosureLightsOn()) { enclosure.switchEnclosureLightsOff(); } else { enclosure.switchEnclosureLightsOn(); }
        }
        zooDataHandler.updateEnclosure(enclosure);
        repaint();
    }


    @Override
    public void paintComponent(Graphics g) {
        super.paintComponent(g);

        //PAINT BACKGROUND
        g.setColor(Color.WHITE);
        g.fillRect(backgroundLeftSide, backgroundTop, backgroundWidth, backgroundHeight);

        //PAINT ENCLOSURE
        g.setColor(Color.BLACK);
        g.fillRect(enclosureLeftSide, enclosureTop, enclosureWidth, enclosureHeight);

        //PAINT THE DOORS
        for (int i = 0; i < enclosureEntries.size(); i++) {
            if (enclosureEntries.get(i).isEntryOpen()) {
                g.setColor(Color.RED);
            } else {
                g.setColor(Color.GREEN);
            }

            switch (i) {
                case 0:
                    g.fillRect(doorOneLeftSide, doorOneTop, doorOneWidth, doorOneHeight);
                    break;
                case 1:
                    g.fillRect(doorTwoLeftSide, doorTwoTop, doorTwoWidth, doorTwoHeight);
                    break;
                case 2:
                    g.fillRect(doorThreeLeftSide, doorThreeTop, doorThreeWidth, doorThreeHeight);
                    break;
            }
        }

        //PAINT THE ANIMAL INDICATOR
        if (enclosure.isEnclosureOccupiedByAnimal()) {
            g.setColor(Color.RED);
        } else {
            g.setColor(Color.GREEN);
        }
        g.drawRect(animalIndicatorLeftSide, animalIndicatorTop, animalIndicatorWidth, animalIndicatorHeight);

        //PAINT THE HUMAN INDICATOR
        if (enclosure.isEnclosureOccupiedByPerson()) {
            g.setColor(Color.RED);
        } else {
            g.setColor(Color.GREEN);
        }
        g.drawRect(humanIndicatorLeftSide, humanIndicatorTop, humanIndicatorWidth, humanIndicatorHeight);

        //PAINT THE LIGHTS INDICATOR
        if (enclosure.isEnclosureLightsOn()) {
            g.setColor(Color.YELLOW);
        } else {
            g.setColor(Color.BLUE);
        }
        g.drawLine(75, 142, 75, 132);
        g.drawLine(75, 142, 70, 132);
        g.drawLine(75, 142, 65, 132);
        g.drawLine(75, 142, 60, 132);
        g.drawLine(75, 142, 80, 132);
        g.drawLine(75, 142, 85, 132);
        g.drawLine(75, 142, 90, 132);

        //PAINT THE ENCLOSURE LOCK
        if (enclosure.isEnclosureLocked()) {
            g.setColor(Color.GREEN);
            g.drawArc(122, 130, 12, 12, 0, 180);
            g.drawRect(122, 135, 12, 7);
        } else {
            g.setColor(Color.RED);
            g.drawArc(110, 130, 12, 12, 0, 180);
            g.drawRect(122, 135, 12, 7);
        }
    }

}
